import RandomUtil from "./RandomUtil";

/**
 * 处理玩家更改内存问题,如果要变量防止玩家更改内存可以继承这个败类
 */
export class SafeDataModel {

    public static instance: SafeDataModel = new SafeDataModel();

    private dataKeyMap: Map<any, number> = new Map<any, number>();
    private dataMap: Map<any, number> = new Map<any, number>();

    public getNumber(key: any) {
        let offest: number = this.dataKeyMap.get(key);
        let value: number = this.dataMap.get(key);
        if (isNaN(value)) {
            value = 0;
        }
        value += offest;
        return value;
    }

    public setNumber(key: any, value: number) {
        let randomOffset: number = RandomUtil.nextInt(1, 666666);
        value -= randomOffset;
        this.dataKeyMap.set(key, randomOffset);
        this.dataMap.set(key, value);
    }
}
